package com.redrose.demo.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;

import com.redrose.demo.R;
import com.redrose.demo.bean.ListItem;
import com.redrose.demo.databinding.MainItemLayout2Binding;
import com.redrose.demo.databinding.MainItemLayout3Binding;
import com.redrose.demo.databinding.MainItemLayoutBinding;
import com.redrose.demo.multiple.BaseMultiLayoutAdapter;
import com.redrose.demo.multiple.BaseMultipleViewHolder;
import com.redrose.demo.util.ContextUtil;

import java.util.List;
import java.util.function.Consumer;

public class MainAdapter extends BaseMainAdapter {
    public MainAdapter(List<ListItem> list) {
        super(list);
    }

    @Override
    protected BaseMultipleViewHolder<ListItem> getViewHolder(ViewGroup parent, int viewType) {
        BaseMultipleViewHolder<ListItem> holder = null;
        int layoutResId = getListItem(viewType);
        View viewByLayoutRes = getViewByLayoutRes(parent,layoutResId);
        switch (viewType) {
            case 0:
                holder = new MainViewHolder(parent, viewByLayoutRes);
                break;
            case 1:
                holder = new MainViewHolder2(parent, viewByLayoutRes);
                break;
            case 2:
                holder = new MainViewHolder3(parent, viewByLayoutRes);
                break;
            default:
                break;
        }
        return holder;
    }

    // TODO 有个缺陷，如何解决每一项Item的问题
    private int getListItem(int itemType) {
        final int[] layoutRes = new int[1];
        mList.forEach(listItem -> {
            if (listItem.getItemType() == itemType) {
                layoutRes[0] = listItem.getLayoutResId();
            }
        });
        return layoutRes[0];
    }
}
